package com.item.service.Impl;

import com.item.mapper.AdminMapper;
import com.item.pojo.Admin;
import com.item.service.AdminService;
import com.item.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;



    @Override
    public Admin getById(String id) {
        return adminMapper.getById(id);
    }

    @Override
    public Admin login(Admin admin) {

        //数据库查询出来的对象
        Admin admin1 = adminMapper.selectByName(admin.getAdminName());

        // 设置token
        String token = TokenUtils.genToken(admin1.getId().toString(),admin1.getPassword());
        admin1.setToken(token);

        if (admin1.getPassword().equals(admin.getPassword())){
            //更新数据库
            adminMapper.update(admin1);
            return admin1;
        }
        return null;
    }

    @Override
    public Admin update(Admin admin) {
        adminMapper.update(admin);
        return adminMapper.selectByName(admin.getAdminName());
    }

    @Override
    public Admin selectByName(String adminName) {
        return adminMapper.selectByName(adminName);
    }
}
